package

{

	import fl.controls.Button;

	import fl.controls.TextInput;

	import flash.display.Sprite;

	import flash.events.SyncEvent;

	import flash.events.NetStatusEvent;

	import flash.events.MouseEvent;

	import flash.net.SharedObject;

	import flash.net.NetConnection;

	import flash.net.URLRequest;

	import flash.net.navigateToURL;



	public class SharedFunction extends Sprite
	{

		private var button:Button;

		private var web_so:SharedObject;

		private var nc:NetConnection;

		private var textInput:TextInput;

		private var rtmpGo:String;

		private var webURL:String;

		private var good:Boolean;

		private var webRequest:URLRequest;



		public function SharedFunction()

		{

			//Set up UIs



			textInput = new TextInput();

			textInput.setSize(150, 24);

			textInput.move(20, 50);

			addChild(textInput);



			button = new Button();

			button.width = 150;

			button.label = "Open Web Page";

			button.move(20, 80);

			button.addEventListener(MouseEvent.CLICK, sendWeb);

			addChild(button);



			//NetConnection

			rtmpGo = "rtmp://192.168.1.209/test";

			nc = new NetConnection();

			nc.connect(rtmpGo);

			nc.addEventListener(NetStatusEvent.NET_STATUS, doSO);

		}



		private function doSO(e:NetStatusEvent):void

		{

			good = e.info.code == "NetConnection.Connect.Success";

			if (good)

			{

				//Set up shared object

				web_so = SharedObject.getRemote("web", nc.uri, false);

				web_so.connect(nc);

				web_so.addEventListener(SyncEvent.SYNC, checkSO);

			}

		}



		private function checkSO(e:SyncEvent):void

		{

			for (var chng:uint; chng < e.changeList.length; chng++)

			{

				switch (e.changeList[chng].code)

				{

					case "clear":

						break;



					case "success":

						trace(web_so.data.web);

						break;



					case "change":

						webRequest = new URLRequest(web_so.data.web);

						navigateToURL(webRequest, "_blank");

						break;

				}

			}

		}

		private function sendWeb(e:MouseEvent):void
		{
			web_so.setProperty("web", "http://" + textInput.text);

		}

	}

}
